package com.ruoyi.yipaiju.service.impl;

import com.ruoyi.yipaiju.domain.DatavPoint;
import com.ruoyi.yipaiju.mapper.DatavMapper;
import com.ruoyi.yipaiju.service.IDatavService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class DatavServiceImpl implements IDatavService {

    @Autowired
    private DatavMapper datavMapper;

    @Override
    public Map<String, String> getBasicsInfo() {
        Map<String, String> basicsInfo = datavMapper.getBasicsInfo();

        return basicsInfo;
    }

    @Override
    public List<Map<String, Object>> getInvitePeople(String queryTime) {
        List<Map<String, String>> invitePeople = datavMapper.getInvitePeople(queryTime);
        Map<String, Map<String, String>> day_value = invitePeople.stream().collect(Collectors.toMap(item -> item.get("day_value"), item -> item));
        List<Map<String,Object>> seriesData = new ArrayList<>();
        try {

            Map<String,Object> map1 = new HashMap<>();
            Map<String,Object> map2 = new HashMap<>();
            map1.put("name","无手机号");
            map1.put("type","line");
//            map1.put("data","line");
            map2.put("name","有手机号");
            map2.put("type","line");
//            map2.put("data","line");
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            Date parse = df.parse(queryTime);
            Calendar cal = Calendar.getInstance();
            cal.setTime(parse);
            List<Object []> data1 = new ArrayList<>();
            List<Object []> data2 = new ArrayList<>();
            for (int i = 0 ;i<7; i++){
                String format = df.format(cal.getTime());
                Map<String, String> stringStringMap = day_value.get(format);
                if(ObjectUtils.isNotEmpty(stringStringMap)){
                    data1.add(new Object[]{format,stringStringMap.get("unphone_count")});
                    data2.add(new Object[]{format,stringStringMap.get("phone_count")});
                }else {
                    data1.add(new Object[]{format,0});
                    data2.add(new Object[]{format,0});
                }
                cal.add(Calendar.DAY_OF_MONTH, 1);
            }
            map1.put("data",data1);
            map2.put("data",data2);
            seriesData.add(map1);
            seriesData.add(map2);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return seriesData;
    }

    @Override
    public List<Map<String, Object>> getProbability(int diffTime, String queryTime, String province, String city, String district) {
        List<Map<String, String>> probability = datavMapper.getProbability(queryTime, province, city, district);
        Map<String, Map<String, String>> day_value = probability.stream().collect(Collectors.toMap(item -> item.get("month_value"), item -> item));
        List<Map<String,Object>> seriesData = new ArrayList<>();
        try {

            Map<String,Object> map1 = new HashMap<>();
            Map<String,Object> map2 = new HashMap<>();
            map1.put("name","折扣");
            map1.put("type","line");
//            map1.put("data","line");
            map2.put("name","概率");
            map2.put("type","line");
//            map2.put("data","line");
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
            Date parse = df.parse(queryTime);
            Calendar cal = Calendar.getInstance();
            cal.setTime(parse);
            List<Object []> data1 = new ArrayList<>();
            List<Object []> data2 = new ArrayList<>();
            for (int i = 0 ;i<diffTime; i++){
                cal.add(Calendar.MONTH, 1);
                String format = df.format(cal.getTime());
                Map<String, String> stringStringMap = day_value.get(format);
                if(ObjectUtils.isNotEmpty(stringStringMap)){
                    data1.add(new Object[]{format,stringStringMap.get("deal_discount")});
                    data2.add(new Object[]{format,stringStringMap.get("deal_rate")});
                }else {
                    data1.add(new Object[]{format,0});
                    data2.add(new Object[]{format,0});
                }
//                cal.add(Calendar.DAY_OF_MONTH, 1);
            }
            map1.put("data",data1);
            map2.put("data",data2);
            seriesData.add(map1);
            seriesData.add(map2);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return seriesData;
    }

    @Override
    public List<Map<String, Object>> getAuctionNum(int diffTime, String queryTime, String province, String city, String district) {
        List<Map<String, String>> probability = datavMapper.getAuctionNum(queryTime, province, city, district);
        Map<String, Map<String, String>> day_value = probability.stream().collect(Collectors.toMap(item -> item.get("month_value"), item -> item));
        List<Map<String,Object>> seriesData = new ArrayList<>();
        try {

            Map<String,Object> map1 = new HashMap<>();
            Map<String,Object> map2 = new HashMap<>();
            map1.put("name","挂拍");
            map1.put("type","bar");
            map1.put("barWidth",20);
            Map<String,Object> itemStyle = new HashMap<>();
            itemStyle.put("color","#c08a4d");
            map1.put("itemStyle",itemStyle);
//            map1.put("data","line");
            map2.put("name","成交");
            map2.put("type","bar");
            map2.put("barWidth",20);
            Map<String,Object> itemStyle2 = new HashMap<>();
            itemStyle2.put("color","#164d30");
            map2.put("itemStyle",itemStyle2);
//            map2.put("data","line");
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
            Date parse = df.parse(queryTime);
            Calendar cal = Calendar.getInstance();
            cal.setTime(parse);
            List<Object []> data1 = new ArrayList<>();
            List<Object []> data2 = new ArrayList<>();
            for (int i = 0 ;i<diffTime; i++){
                cal.add(Calendar.MONTH, 1);
                String format = df.format(cal.getTime());
                Map<String, String> stringStringMap = day_value.get(format);
                if(ObjectUtils.isNotEmpty(stringStringMap)){
                    data1.add(new Object[]{format,stringStringMap.get("doing_count")});
                    data2.add(new Object[]{format,stringStringMap.get("deal_count")});
                }else {
                    data1.add(new Object[]{format,0});
                    data2.add(new Object[]{format,0});
                }
//                cal.add(Calendar.MONTH, 1);
            }
            map1.put("data",data1);
            map2.put("data",data2);
            seriesData.add(map1);
            seriesData.add(map2);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return seriesData;
    }

    @Override
    public List<Map<String, Object>> getAuctionMoney(int diffTime, String queryTime, String province, String city, String district) {
        List<Map<String, String>> probability = datavMapper.getAuctionMoney(queryTime, province, city, district);
        Map<String, Map<String, String>> day_value = probability.stream().collect(Collectors.toMap(item -> item.get("month_value"), item -> item));
        List<Map<String,Object>> seriesData = new ArrayList<>();
        try {

            Map<String,Object> map1 = new HashMap<>();
            Map<String,Object> map2 = new HashMap<>();
            map1.put("name","挂拍");
            map1.put("type","bar");
            map1.put("barWidth",20);
            Map<String,Object> itemStyle = new HashMap<>();
            itemStyle.put("color","#c08a4d");
            map1.put("itemStyle",itemStyle);
//            map1.put("data","line");
            map2.put("name","成交");
            map2.put("type","bar");
            map2.put("barWidth",20);
            Map<String,Object> itemStyle2 = new HashMap<>();
            itemStyle2.put("color","#164d30");
            map2.put("itemStyle",itemStyle2);
//            map2.put("data","line");
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
            Date parse = df.parse(queryTime);
            Calendar cal = Calendar.getInstance();
            cal.setTime(parse);
            List<Object []> data1 = new ArrayList<>();
            List<Object []> data2 = new ArrayList<>();
            for (int i = 0 ;i<diffTime; i++){
                cal.add(Calendar.MONTH, 1);
                String format = df.format(cal.getTime());
                Map<String, String> stringStringMap = day_value.get(format);
                if(ObjectUtils.isNotEmpty(stringStringMap)){
                    data1.add(new Object[]{format,stringStringMap.get("doing_price")});
                    data2.add(new Object[]{format,stringStringMap.get("deal_price")});
                }else {
                    data1.add(new Object[]{format,0});
                    data2.add(new Object[]{format,0});
                }

            }
            map1.put("data",data1);
            map2.put("data",data2);
            seriesData.add(map1);
            seriesData.add(map2);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return seriesData;
    }



    @Override
    public List<DatavPoint> selectInviteCountDay(Long minTime, Boolean existPhone) {
        // TODO datavMapper.selectInviteCountDay
        return null;
    }
}
